﻿using System;
using ASPNETVN.PORTAL.Components.UI;
using System.Web;

namespace ASPNETVN.PORTAL.Modules.INTRODUCTION.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Components.Item item = new Components.Item();
            if (this.ItemID > 0)
            {
                item = Components.Item.Load(this.ItemID);
            }

            item.ID = this.ItemID;
            item.Title = txtTitle.Text;
            item.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue);
            item.SortOrder = Convert.ToInt32(txtSortOrder.Text);
            item.Body = HttpUtility.HtmlEncode(txtBody.Value);
            item.InsertUpdate();

            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                PORTAL.Components.Share.Category.BuildDropDownListControl(ref ddlCategory, this.ModuleID);
                if (this.ItemID > 0)
                {
                    Components.Item item = Components.Item.Load(this.ItemID);
                    if (item != null)
                    {
                        txtTitle.Text = item.Title;
                        ddlCategory.SelectedValue = item.CategoryID.ToString();
                        txtSortOrder.Text = item.SortOrder.ToString();
                        txtBody.Value = HttpUtility.HtmlDecode(item.Body);
                    }
                }
            }
        }
    }
}